package com.chaucer.sqlSession;

import com.chaucer.config.BoundSql;
import com.chaucer.io.Configuration;
import com.chaucer.pojo.MappedStatement;
import com.chaucer.utils.GenericTokenParser;
import com.chaucer.utils.ParameterMapping;
import com.chaucer.utils.ParameterMappingTokenHandler;
import com.chaucer.utils.PrepareStatementHandler;


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SimpleExecutor implements Executor {

    public <E> List<E> query(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        //封装PrepareStatement初始化类PrepareStatementHandler
//        Connection connection = configuration.getDataSource().getConnection();
//        String sql = mappedStatement.getSql();
//        BoundSql boundSql = getBoundSql(sql);
//        PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSqlText());
//
//        String parameterType = mappedStatement.getParameterType();
//        Class<?> parameterTypeClass = getClassType(parameterType);
//
//        List<ParameterMapping> parameterMappingList = boundSql.getParameterMappingList();
//        for (int i = 0; i < parameterMappingList.size(); i++) {
//            ParameterMapping parameterMapping = parameterMappingList.get(i);
//            String content = parameterMapping.getContent();
//            Field declaredField = parameterTypeClass.getDeclaredField(content);
//            declaredField.setAccessible(true);
//            Object o = declaredField.get(params[0]);
//            preparedStatement.setObject(i + 1, o);
//        }
        PrepareStatementHandler prepareStatementHandler = new PrepareStatementHandler(configuration, mappedStatement, params);
        PreparedStatement preparedStatement = prepareStatementHandler.getPrepareStatement();
        ResultSet resultSet =  preparedStatement.executeQuery();
        String resultType = mappedStatement.getResultType();
        Class<?> resultTypeClass = getClassType(resultType);

        ArrayList<Object> objects = new ArrayList<>();

        while (resultSet.next()) {
            ResultSetMetaData metaData = resultSet.getMetaData();
            Object o = resultTypeClass.newInstance();
            for (int i = 1; i <=metaData.getColumnCount() ; i++) {
                String columnName = metaData.getColumnName(i);
                Object value = resultSet.getObject(columnName);
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(columnName, resultTypeClass);
                Method writeMethod = propertyDescriptor.getWriteMethod();
                writeMethod.invoke(o, value);
            }
            objects.add(o);
        }
        return (List<E>) objects;
    }

    private Class<?> getClassType(String parameterType) throws ClassNotFoundException {
        if (parameterType != null) {
            Class<?> aClass = Class.forName(parameterType);
            return aClass;
        }
        return null;
    }


    @Override
    public int insert(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        return this.update(configuration, mappedStatement, params);
    }

    @Override
    public int update(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        PrepareStatementHandler prepareStatementHandler = new PrepareStatementHandler(configuration, mappedStatement, params);
        PreparedStatement preparedStatement = prepareStatementHandler.getPrepareStatement();
        return  preparedStatement.executeUpdate();
    }

    @Override
    public int delete(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        return this.update(configuration, mappedStatement, params);
    }
}
